package com.game.message.selfbinary;

import com.game.message.transport.DataBuffer;

/**
 * TCP协议的头文件
 */
public class Header {
    public Header(int messageId) {
        this.messageId = messageId;
    }
    public Header() {
    }
    public static final int PROTOCOL_HEADER_LENGTH = 8;

    private int length; // 数据包长度，包括包头长度
    private int messageId; // ServerStage
    private short commandId; // CID
    private short reserved; // 保留，可用于如序列号等
    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }


    public int getMessageId() {
        return messageId;
    }

    public void setMessageId(int messageId) {
        this.messageId = messageId;
    }

    public short getCommandId() {
        return commandId;
    }

    public void setCommandId(short commandId) {
        this.commandId = commandId;
    }

    public short getReserved() {
        return reserved;
    }

    public void setReserved(short reserved) {
        this.reserved = reserved;
    }

    public DataBuffer encode() {
        DataBuffer data = new DataBuffer(PROTOCOL_HEADER_LENGTH);
        data.writeInt(messageId);
        data.writeShort(commandId);
        data.writeShort(reserved);
        return data;
    }

    public void decode(DataBuffer data) {
        messageId = data.readInt();
        commandId = data.readShort();
        reserved = data.readShort();
    }

    @Override
    public String toString() {
        return "Header{" +
                "length=" + length +
                ", messageId=" + messageId +
                ", commandId=" + commandId +
                ", reserved=" + reserved +
                '}';
    }
}
